package cn.tradewin.advisor.pages.passets.checklist;

import java.util.List;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.ActivationRequestParameter;
import org.apache.tapestry5.annotations.DiscardAfter;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Grid;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.ajax.AjaxResponseRenderer;
import org.slf4j.Logger;

import cn.tradewin.advisor.commons.selectmodel.CodeLabelSelectModel;
import cn.tradewin.advisor.persist.model.CodingChk;
import cn.tradewin.advisor.persist.services.CodingChkService;
import cn.tradewin.advisor.persist.services.PassetsMasterService;

public class CodingBoard {

	@Property
	@ActivationRequestParameter
	private Integer lang;
	
	@Property
	private SelectModel langModel;
	
	@Property
	@ActivationRequestParameter
	private Integer category;
	
	@Property
	private SelectModel categoryModel;
	
	@Property
	private String content;
	
	@Property
	private List<CodingChk> codeChks;
	
	@Property
	private CodingChk codeChk;
	
	@Inject
	private CodingChkService codeChkService;
	
	@Inject
	private PassetsMasterService passetsMasterService;
	
	@InjectComponent
	private Zone categoryZone;
	
	@InjectComponent
	private Zone recordsZone;
	
	@InjectComponent
	private Grid grid;
	
	@Inject
	private Request request;
	
	@Inject
	private Logger log;
	
	@Inject
    private AjaxResponseRenderer ajaxResponseRenderer;
	
	void setupRender() {
		langModel = new CodeLabelSelectModel(passetsMasterService.getLangOptions());
		categoryModel = new CodeLabelSelectModel(passetsMasterService.getCategoryOptions(lang));
		codeChks = codeChkService.getCodingChksByLang(lang, category);
		List<String> ps = grid.getDataModel().getPropertyNames();
		for (String p : ps) {
			log.info("property = " + p);
			grid.getDataModel().get(p).sortable(false);
		}
		grid.getDataModel().get("chkLangName").sortable(false);
		
//		grid.getDataModel().get("category").sortable(false);
//		List<SortConstraint> sorts = grid.getSortModel().getSortConstraints();
//		for(SortConstraint sort : sorts) {
//			sort.getPropertyModel().sortable(false);
//		}
	}
	
	Object onSelectedFromAdd() {
		CodingChk item = new CodingChk();
		item.setChkLang(lang);
		item.setChkCategory(category);
		item.setChkContent(content);
		codeChkService.addCheckItem(item);
		codeChks = codeChkService.getCodingChksByLang(lang, category);
		return request.isXHR() ? recordsZone.getBody() : this;
	}
	
	void onValueChangedFromLang(Integer lang) {
		 if (request.isXHR()) {
			categoryModel = new CodeLabelSelectModel(passetsMasterService.getCategoryOptions(lang));
			ajaxResponseRenderer.addRender(categoryZone);
	    }
	}
	
	Object onSelectedFromSearch() {
		codeChks = codeChkService.getCodingChksByLang(lang, category);
		return request.isXHR() ? recordsZone.getBody() : this;
	}
		
	void onDelete(Integer chkCode) {
		codeChkService.delete(chkCode);
	}
	
	@DiscardAfter
	void destroy() {
		
	}
}